<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * Main controller for showing the index
 */
class Articles extends CI_Controller {

    public $data;

    function __construct() {
        parent::__construct();
        // CHECK LOGIN                     << START >>
        $is_logged_in = $this->session->userdata('is_logged_in');
        if (!isset($is_logged_in) || $is_logged_in != TRUE) {
            echo "Unauthorized... Redirecting to login site.";
            redirect('admin/login', 'refresh');
            die();
        }
        // CHECK LOGIN                      << END >>
        // load session data to $data
        $this->data = $this->session->all_userdata();
        // load article model
        $this->load->model('article');
    }

    /**
     * Átirányít az article listához
     */
    function index() {
        $this->article_list();
    }

    /**
     * Article lista
     */
    function article_list() {
        $this->load->helper('html');
        $this->load->helper('text');
        //$this->load->library('pagination');

        $this->data['title'] = "Article list";
        $this->data['current_page'] = "article_list";

        // ha volt flashdata a sessionbe, akkor az kiírjuk message-nek.
        $this->data['message'] = $this->session->flashdata('message');

        // article lehívása

        $this->data['articles'] = $this->article->get_articles();
        // a menüt itt gonoszul lehívom returnbe.
        $this->data['menu'] = $this->load->view('admin/menu', $this->data, TRUE);

        $this->load->view('admin/header', $this->data);
        $this->load->view('admin/article_list', $this->data);
        $this->load->view('admin/footer');
    }

    /**
     * Új article
     * @param type $id 
     */
    function edit_new() {
        $this->load->helper('form');
        $this->load->helper('html');

        // oldal info
        $this->data['title'] = "Article edit";
        $this->data['current_page'] = "article_edit";

        // a menüt itt gonoszul lehívom returnbe.
        $this->data['menu'] = $this->load->view('admin/menu', $this->data, TRUE);

        $this->load->view('admin/header', $this->data);
        $this->load->view('admin/article_edit', $this->data);
        $this->load->view('admin/footer');
    }

    /**
     * Article szerkesztő
     * @param type $id 
     */
    function edit($id) {
        // ha nincs id elküldjük újat szerkeszteni
        if (!isset($id))
            $this->edit_new();

        $this->load->helper('form');
        $this->load->helper('html');

        $this->data['title'] = "Article edit";
        $this->data['current_page'] = "article_edit";

        // retrieve article
        $this->data['article'] = $this->article->retrieve($id);
 
        // a menüt itt gonoszul lehívom returnbe.
        $this->data['menu'] = $this->load->view('admin/menu', $this->data, TRUE);

        $this->load->view('admin/header', $this->data);
        $this->load->view('admin/article_edit', $this->data);
        $this->load->view('admin/footer');
    }

    /**
     * Delete article
     * @param type $id 
     */
    function delete($id) {
        $this->article->delete($id);
        $this->session->set_flashdata('message', "$id. cikk törölve.");
        redirect('admin/articles', 'refresh');
    }

    /**
     * Új article hozzáadása
     */
    function add_new() {
        // add id, title, text
        $id = $this->session->userdata('id');
        $title = addslashes($this->input->post('title', TRUE));
        $text = addslashes($this->input->post('text', TRUE));
        $this->article->add($id, $title, $text);
        // FRISSÍTVE - SZÖVEG
        $this->session->set_flashdata("Új cikk hozzáadva.");
        redirect('/admin/articles', 'refresh');
    }

    /**
     * Article frissítése
     */
    function update() {
        $id = $this->input->post('id', TRUE);
        $title = addslashes($this->input->post('title', TRUE));
        $text = addslashes($this->input->post('text', TRUE));

        $this->article->update($id, $title, $text);
        $this->session->set_flashdata('message', "$id. cikk frissítve.");
        redirect('/admin/articles', 'refresh');
    }

    /**
     * Ajax kérés $idjű article 
     * @param type $id 
     */
    function ajax_get_article($id = NULL) {
        if(!isset($id)) die("ID REQUED");
        $data = $this->article->retrieve($id);
        $json_data = json_encode($data);
        print_r($json_data);
    }

}